﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Keepo.Models;
using Keepo.Helpers;
using AlbumDesigner.Models.Enums;
using AlbumDesigner.Helpers;
using Keepo.Models.Services;

namespace AlbumDesigner.Controllers
{
    public class TipsController : Controller
    {
        private AlbumContext db = new AlbumContext();

        [CheckPermission(section = "Tips")]
        public ActionResult Index()
        {
            return View();
        }

        [CheckPermission(section = "Tips")]
        public ActionResult GetTipsList()
        {
            var tips = db.Tips.ToList();

            return Json(tips);
        }

        [CheckPermission(section = "Tips")]
        public ActionResult Update(Tip tip)
        {
            var existingTip = db.Tips.FirstOrDefault(m => m.TipId == tip.TipId);

            //the tip exists in DB
            if (existingTip != null)
            {
                //update the relevant fields
                existingTip.TipText = tip.TipText;

                db.SaveChanges();

                return Json(existingTip.TipId);
            }
            //the tip is new
            else
            {
                var newTip = new Tip();
                newTip.TipText = tip.TipText;

                //insert this new Tip
                db.Tips.Add(newTip);

                db.SaveChanges();

                return Json(newTip.TipId);
            }
        }

        [CheckPermission(section = "Tips")]
        public ActionResult Delete(int id)
        {
            var existingTip = db.Tips.FirstOrDefault(m => m.TipId == id);

            //delete the specified Tip
            db.Tips.Remove(existingTip);

            db.SaveChanges();

            return Json(new KeyValuePair<bool, string>(true, "הטיפ נמחק בהצלחה"));
        }

        // Flex
        public ActionResult GetDesignTips()
        {
            return Json(AlbumsLogic.getDesignTips(), JsonRequestBehavior.AllowGet);
        }
    }
}
